﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Security.Application;

namespace ESAM_Assignment_3
{
    public partial class EditComment : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int CommentID;

                //string commentID = Encryption.DecryptQueryString((string)HttpUtility.UrlDecode(Request.QueryString["CommentID"]));
                
                if (Int32.TryParse(Encryption.DecryptQueryString(HttpUtility.UrlDecode(Request.QueryString["CommentID"])), out CommentID) && CommentClass.GetComment(CommentID).Username == User.Identity.Name)
                {
                    CommentClass myComment = CommentClass.GetComment(CommentID);

                    if (myComment != null)
                    {
                        tbxComments.Text = HttpUtility.HtmlDecode(myComment.Comment);
                        tbxSubject.Text = HttpUtility.HtmlDecode(myComment.Subject);
                        lblDatePosted.Text = myComment.DatePosted;
                        lblCommentID.Text = CommentID.ToString();
                        lblFullname.Text = myComment.Fullname;
                    }
                }
            }
        }

        protected void btnUpdateComment_Click(object sender, EventArgs e)
        {
            int CommentID = Convert.ToInt32(lblCommentID.Text);
            string comment = AntiXss.HtmlEncode(tbxComments.Text);
            string subject = AntiXss.HtmlEncode(tbxSubject.Text);
            comment = HtmlEnabler.Enable(comment);
            subject = HtmlEnabler.Enable(subject);
            string datePosted = Common.InsertTodayIntoCommentsDate();

            bool status = CommentClass.UpdateComment(CommentID, comment, datePosted, subject);

            if (status)
            {
                lblStatus.Text = "Comment updated successfully.";
                lblStatus.ForeColor = System.Drawing.Color.Blue;
            }
            else
            {
                lblStatus.Text = "An error occurred. Your comment was not updated.";
                lblStatus.ForeColor = System.Drawing.Color.Red;
            }
        }
    }
}